﻿using Levfier.Proccessors.Filters;
using Levfier.Proccessors.Parameters;
using System;

namespace Levfier.Proccessors.EQs
{
    /// <summary>
    /// Fender標準の3バンドイコライザー
    /// </summary>
    public class ThirtyBandEQ : ProccessorSetBase
    {
        /// <summary>
        /// クオリティファクタ
        /// </summary>
        private static readonly double DefaultQFuctor = 3.0d;

        /// <summary>
        /// 周波数の一覧[Hz]
        /// </summary>
        private static readonly double[] Freqencies = 
        {
            25, 31, 39, 50, 62, 79, 100, 125, 158, 200,
            251, 317, 400, 503, 634, 800, 1000, 1200, 1500, 2000,
            2500, 3200, 4000, 5000, 6000, 8000, 10000, 12000, 16000, 20000
        };

        /// <summary>
        /// フィルタの配列
        /// </summary>
        private PeakIIR[] peaks;

        /// <summary>
        /// 処理を行う
        /// </summary>
        /// <param name="input">入力</param>
        public override void Proccess(double[] input)
        {
            if(null == this.lowerChain)
            { 
                return;
            }

            if (this.bypass)
            {
                this.ProccessBypass(input, this.lowerChain.GetBuffer());
                this.lowerChain.FlowSounds();
            }
            else
            {
                //最後尾のhighにバッファチェインを設定
                this.peaks[Freqencies.Length - 1].SetLowerChain(this.lowerChain);

                // 最初のフィルタに音を流す
                this.peaks[0].Proccess(input);
            }

            //↓自動的に呼び出される
            //後段のストリームへ流す
            //lowerChain.FlowSounds();
        }

        /// <summary>
        /// 使用しない
        /// </summary>
        /// <param name="inputBuffer">入力</param>
        /// <param name="outputBuffer">出力</param>
        protected override void InnerProccess(double[] inputBuffer, double[] outputBuffer)
        {
            //なにもしない
        }

        /// <summary>
        /// パラメータの初期化を行う
        /// </summary>
        protected override void InitParameters()
        {
            //イコライザを構成する
            this.peaks = new PeakIIR[Freqencies.Length];

            //各イコライザを初期化する
            for (int i = 0; i < Freqencies.Length; i++)
            {
                 //生成
                this.peaks[i] = new PeakIIR();

                //固定パラメータ設定
                this.peaks[i].GetParameters()["Freq[Hz]"].MaxValue = 20000.0d;  //20kHzまで設定可能にする
                this.peaks[i].GetParameters()["Freq[Hz]"].Value = Freqencies[i];
                this.peaks[i].GetParameters()["QFactor"].Value = DefaultQFuctor;

                //表示用の名前に変更
                if (Freqencies[i] < 1000)
                {
                    this.peaks[i].GetParameters()["Gain[dB]"].Name = Freqencies[i].ToString("F0");
                }
                else
                {
                    this.peaks[i].GetParameters()["Gain[dB]"].Name = (Freqencies[i] / 1000d).ToString("F1") + "k";
                }
                //パラメータ登録
                this.parameters.Add(this.peaks[i].GetParameters()["Gain[dB]"].Name, this.peaks[i].GetParameters()["Gain[dB]"]);
            }

            //接続する
            for (int i = 0; i < Freqencies.Length - 1; i++)
            {
                this.ChainProccessors(this.peaks[i], this.peaks[i+1]);
            } 
        }
    }
}
